#!/bin/bash
exec 2>install.log
function insertMenu {
	FILE=$1
	POSITION=$2
	SOURCE=""
	WHEREAMI=`dirname $0`

	if [ -e "/usr/share/mythtv/${FILE}.xml" ]; then
		SOURCE="/usr/share/mythtv/${FILE}.xml"
	elif [ -e "/usr/local/share/mythtv/${FILE}.xml" ]; then
		SOURCE="/usr/local/share/mythtv/${FILE}.xml"
	fi

	#Check the file to inject exists
	if [ -e "${WHEREAMI}/${FILE}_menu_item.xml" ]; then

		#Check the target file exists
		if [ "${SOURCE}" != "" ]; then

			#If the iplayer menu item has not already been added
			if [ `grep -c "mythiplayer" ${SOURCE}` -lt 1 ]; then

				#Add the menu item to position $POSITION
				${WHEREAMI}/add_to_menu.sh ${SOURCE} ${WHEREAMI}/${FILE}_menu_item.xml ${POSITION}
				if [ $? -eq 0 ]; then 
					echo -e "\e[0;32m[ OK ]\e[0m Menu item added to ${FILE}.xml"
				else
					echo -e "\e[0;31m[ FAILED ]\e[0m Menu item not added to ${FILE}.xml"
				fi

			else
				echo -e "\e[0;32m[ OK ]\e[0m Menu already contains iPlayer item in ${FILE}.xml"
			fi
		else
			echo -e "\e[0;31m[ FAILED ]\e[0m Unable to find ${FILE}.xml in either /usr/share/mythtv or /usr/local/share/mythtv"
		fi
	else
		echo -e "\e[0;31m[ FAILED ]\e[0m Unable to find ${WHEREAMI}/${FILE}_menu_item.xml"	
	fi
}

insertMenu library 2
insertMenu util_menu 3
insertMenu media_settings 3
